GAE Query Filter, Sort, Limit, Paging

Categories: Development Notes; Tagged with: ; @ April 20th, 2011 22:37

GAE关于Java的资料这是不多, 而且多数都要翻墙, 昨晚做个分页, 差点累死啊!

闲言碎语不多说, 上代码:

DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
		Query query = new Query("User");
		query.addFilter("name", FilterOperator.NOT_EQUAL, ""); // 设置Filter
		query.addSort("name", SortDirection.ASCENDING); // 要设置Sort, 则Filter的Attribute必须Sort, 且要放在其他Sort之前(Attribute要Index)
		query.addSort("ip", SortDirection.DESCENDING); // 设置Sort, 要在Filter对应Attribute的Sort之后
		PreparedQuery pq = datastoreService.prepare(query);
		List listUsers = pq.asList(FetchOptions.Builder.withLimit(2)); // 设置Limit
 
Properties in Inequality Filters Must Be Sorted before Other Sort Orders

If a query has both a filter with an inequality comparison and one or more sort orders, the query must include a sort order for the property used in the inequality, and the sort order must appear before sort orders on other properties.

This query is not valid, because it uses an inequality filter and does not order by the filtered property;

文档: http://code.google.com/appengine/docs/java/datastore/queries.html#Restrictions_on_Queries

翻不了墙的同学可以看这个 – 主要是分页: 

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Cursor;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.QueryResultList;

public class ListPeopleServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {

        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Query q = new Query("Person");
        PreparedQuery pq = datastore.prepare(q);
        int pageSize = 15;

        resp.setContentType("text/html");
        resp.getWriter().println("
    "); FetchOptions fetchOptions = FetchOptions.Builder.withLimit(pageSize); String startCursor = req.getParameter("cursor"); // If this servlet is passed a cursor parameter, let's use it if (startCursor != null) { fetchOptions.startCursor(Cursor.fromWebSafeString(startCursor)); } QueryResultList results = pq.asQueryResultList(fetchOptions); for (Entity entity : results) { resp.getWriter().println("
  • " + entity.getProperty("name") + ""); } resp.getWriter().println("
"); String cursor = results.getCursor().toWebSafeString(); // Assuming this servlet lives at '/people' resp.getWriter().println( "Next page"); } }

 

关于分页的另一篇:

Implementing Paging on Google App Engine Java (gae/j) and GWT — the query cursor way

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.